import http from './http'
import { requestWithRetry } from '@/utils/retry'
import type { Transaction, SalesOrder, MatchResult } from '@/types/workbench'
import type { ReconciliationResult, ReconciliationQueryParams } from '@/types/reconciliation'

// 对账API服务
export const reconciliationAPI = {
  // 获取待匹配数据
  async getPendingData() {
    try {
      // 使用重试机制获取待匹配数据
      const response: any = await requestWithRetry(
        () => http.get('/reconciliation/pending-data'),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: {
          list: response.data?.list || [],
          pagination: response.data?.pagination || {
            page: 1,
            limit: 20,
            total: 0
          }
        }
      };
    } catch (error: any) {
      return {
        success: false,
        data: {
          list: [],
          pagination: null
        },
        message: error.message || '获取待匹配数据失败'
      };
    }
  },

  // 智能匹配
  async smartMatch(algorithm: string = 'comprehensive', options: any = {}) {
    try {
      // 使用重试机制进行智能匹配
      const response: any = await requestWithRetry(
        () => http.post('/reconciliation/smart-match', {
          algorithm,
          options
        }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: {
          list: response.data?.list || [],
          pagination: response.data?.pagination || {
            page: 1,
            limit: 20,
            total: 0
          }
        }
      };
    } catch (error: any) {
      return {
        success: false,
        data: {
          list: [],
          pagination: null
        },
        message: error.message || '智能匹配失败'
      };
    }
  },

  // 手动匹配
  async manualMatch(transactionIds: string[], orderIds: string[], algorithm: string = 'comprehensive') {
    try {
      // 使用重试机制进行手动匹配
      const response: any = await requestWithRetry(
        () => http.post('/reconciliation/manual-match', {
          transactionIds,
          orderIds,
          algorithm
        }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: {
          list: response.data?.list || [],
          pagination: response.data?.pagination || {
            page: 1,
            limit: 20,
            total: 0
          }
        }
      };
    } catch (error: any) {
      return {
        success: false,
        data: {
          list: [],
          pagination: null
        },
        message: error.message || '手动匹配失败'
      };
    }
  },

  // 确认匹配
  async confirmMatch(id: string) {
    try {
      // 使用重试机制确认匹配
      const response: any = await requestWithRetry(
        () => http.put(`/reconciliation/confirm/${id}`),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: {
          list: response.data?.list || [],
          pagination: response.data?.pagination || {
            page: 1,
            limit: 20,
            total: 0
          }
        }
      };
    } catch (error: any) {
      return {
        success: false,
        data: {
          list: [],
          pagination: null
        },
        message: error.message || '确认匹配失败'
      };
    }
  },
  
  // 拒绝匹配
  async rejectMatch(id: string) {
    try {
      // 使用重试机制拒绝匹配
      const response: any = await requestWithRetry(
        () => http.put(`/reconciliation/reject/${id}`),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: {
          list: response.data?.list || [],
          pagination: response.data?.pagination || {
            page: 1,
            limit: 20,
            total: 0
          }
        }
      };
    } catch (error: any) {
      return {
        success: false,
        data: {
          list: [],
          pagination: null
        },
        message: error.message || '拒绝匹配失败'
      };
    }
  },
  
  // 重新对账 - 选择性重新处理已确认的匹配记录
  async reReconcile(matchIds?: number[]) {
    try {
      // 使用重试机制重新对账
      const response: any = await requestWithRetry(
        () => http.post('/reconciliation/rerun', {
          matchIds
        }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: {
          list: response.data?.list || [],
          pagination: response.data?.pagination || {
            page: 1,
            limit: 20,
            total: 0
          }
        }
      };
    } catch (error: any) {
      return {
        success: false,
        data: {
          list: [],
          pagination: null
        },
        message: error.message || '重新对账失败'
      };
    }
  },

  // 生成智能分析报告
  async generateAnalysisReport(params: any = {}) {
    try {
      // 使用重试机制生成智能分析报告
      const response: any = await requestWithRetry(
        () => http.get('/reconciliation/generate-report', { params }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: {
          list: response.data?.list || [],
          pagination: response.data?.pagination || {
            page: 1,
            limit: 20,
            total: 0
          }
        }
      };
    } catch (error: any) {
      return {
        success: false,
        data: {
          list: [],
          pagination: null
        },
        message: error.message || '生成智能分析报告失败'
      };
    }
  },

  // 获取对账结果列表
  async getReconciliationResults(params: ReconciliationQueryParams) {
    try {
      // 使用重试机制获取对账结果列表
      const response: any = await requestWithRetry(
        () => http.get('/reconciliation/results', { params }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: {
          list: response.data?.list || [],
          pagination: response.data?.pagination || {
            page: 1,
            limit: 20,
            total: 0
          }
        }
      };
    } catch (error: any) {
      return {
        success: false,
        data: {
          list: [],
          pagination: null
        },
        message: error.message || '获取对账结果列表失败'
      };
    }
  },

  // 获取对账统计信息
  async getReconciliationStats() {
    try {
      // 使用重试机制获取对账统计信息
      const response: any = await requestWithRetry(
        () => http.get('/reconciliation/stats'),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: {
          total: 0,
          matched: 0,
          unmatched: 0,
          confirmed: 0,
          rejected: 0
        },
        message: error.message || '获取对账统计信息失败'
      };
    }
  },

  // 导出对账结果
  async exportReconciliationResults(params: any) {
    try {
      const response = await http.get('/reconciliation/export', { 
        params,
        responseType: 'blob'
      });
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '导出对账结果失败'
      };
    }
  },
  
  // 获取对账报告数据 - 新增方法
  async getReconciliationReportData(params: any = {}) {
    try {
      // 使用重试机制获取对账报告数据
      const response: any = await requestWithRetry(
        () => http.get('/reconciliation/report-data', { params }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取对账报告数据失败'
      };
    }
  },

  // 获取银行流水记录 - 新增方法
  async getBankTransactions(params: any = {}) {
    try {
      // 使用重试机制获取银行流水记录
      const response: any = await requestWithRetry(
        () => http.get('/bank-transactions', { params }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: {
          list: response.data?.list || [],
          pagination: response.data?.pagination || {
            page: 1,
            limit: 20,
            total: 0
          }
        }
      };
    } catch (error: any) {
      return {
        success: false,
        data: {
          list: [],
          pagination: null
        },
        message: error.message || '获取银行流水记录失败'
      };
    }
  }
};